package com.wandian.news.mvvm.wxtop.wxtoppage;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.mylib.libcore.mvvm.BaseMVVMFragment;
import com.mylib.libcore.utils.UIUtils;
import com.wandian.news.R;
import com.wandian.news.adapter.WXTopRecycleAdapter;
import com.wandian.news.databinding.NewsFragmentToutiaoChildBinding;
import com.wandian.news.databinding.NewsFragmentWxTopPageBinding;


/**
 * @author Administrator
 * @date 2019/12/16-13:30
 * @description 微信热文-子页面
 **/
public class WXTopPageFragment extends BaseMVVMFragment<WXTopPageFragmentViewModel> implements BaseQuickAdapter.RequestLoadMoreListener{

    private NewsFragmentWxTopPageBinding dataBinding;
    private WXTopRecycleAdapter touTiaoChildRecycleAdapter;
    private int page =1;
    private String title;
    public static WXTopPageFragment newInstance(String title) {
        Bundle args = new Bundle();
        args.putString("title", title);
        WXTopPageFragment fragment = new WXTopPageFragment();
        fragment.setArguments(args);
        return fragment;

    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public View initView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        dataBinding = NewsFragmentWxTopPageBinding.inflate(inflater);
        dataBinding.setLifecycleOwner(this);
        return dataBinding.getRoot();
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        dataBinding.setViewModel(mViewModel);

        title = getArguments().getString("title");

        initRecycleView();
        initViewData();
        getData(true);
    }

    private void initViewData(){
        mViewModel.touTiaoContentBean.observe(this,touTiaoContentBean->{

            if(!UIUtils.isEmpty(touTiaoContentBean) && !UIUtils.isEmpty(touTiaoContentBean.getNewslist())){
                if(mViewModel.isReFlash){
                    touTiaoChildRecycleAdapter.setNewData(touTiaoContentBean.getNewslist());
                }else {
                    touTiaoChildRecycleAdapter.addData(touTiaoContentBean.getNewslist());
                    touTiaoChildRecycleAdapter.loadMoreComplete();
                }
            }
            if(!UIUtils.isEmpty(dataBinding.srlRefresh)){
                if(dataBinding.srlRefresh.isRefreshing()){
                    dataBinding.srlRefresh.setRefreshing(false);
                }
            }
        });
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {//视图变为可见
        }
    }

    private void initRecycleView(){

        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext());
        dataBinding.recyclerChild.setLayoutManager(layoutManager);
        touTiaoChildRecycleAdapter  = new WXTopRecycleAdapter(R.layout.news_item_zixun_style_two,title);
        dataBinding.recyclerChild.setAdapter(touTiaoChildRecycleAdapter);

        dataBinding.srlRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getData(true);
            }
        });

        touTiaoChildRecycleAdapter.setOnLoadMoreListener(this, dataBinding.recyclerChild);
    }


    private void getData(boolean isReflsh){

        if(isReflsh){
            page =1;
            if(!UIUtils.isEmpty(touTiaoChildRecycleAdapter) && !UIUtils.isEmpty(touTiaoChildRecycleAdapter.getData())){
                touTiaoChildRecycleAdapter.getData().clear();
                touTiaoChildRecycleAdapter.notifyDataSetChanged();
            }
        }else {
            page++;
        }
        mViewModel.getContentData(title,page,isReflsh);
    }

    @Override
    public void onLoadMoreRequested() {
        getData(false);
    }
}
